﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using PDAMain.BLL;
using PDAMain.DAL;
//using PDAMain.Util;

namespace PDAMain.Forms.RepairWork
{
    public partial class RepairWorkComplexSel : Form
    {
        public RepairWorkComplexSel()
        {
            InitializeComponent();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            //查询
            queryData();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            RepairWorkList list = new RepairWorkList();
            list.Show();
            this.Close();
        }

        private void queryData()
        {
            //维修工单号
            string strWorkNo = string.Empty;
            //资产编号
            string strAssetNo = string.Empty;
            //时间（起止）
            string strDateFrom = string.Empty;
            string strDateTo = string.Empty;

            strWorkNo = txtWoNum.Text.ToString().Trim();
            strAssetNo = txtAssetNo.Text.ToString().Trim();

            try{
                Convert.ToDateTime(dtpFrom.Value.ToString("yyyy/MM/dd hh:mm:ss"));
                strDateFrom = dtpFrom.Value.ToString("yyyy/MM/dd hh:mm:ss");
               }
            catch
            {
                MessageBox.Show("起始时间格式有误，请重新输入");
                return;
            }
            //else
            //{
                
          //  }

            try{ 
                Convert.ToDateTime(dtpTo.Value.ToString("yyyy/MM/dd hh:mm:ss"));
                strDateTo = dtpTo.Value.ToString("yyyy/MM/dd hh:mm:ss");
            }
            catch
            {
                MessageBox.Show("结束时间格式有误，请重新输入");
                return;
            }

            //获取数据
            DataTable dt = new DataTable();
            DataSet ds = null;
           // try
           // {  
               // RepairWorkListDao worklist = new RepairWorkListDao();
               // ds = worklist.complexSel(strWorkNo, strAssetNo, strDateFrom, strDateTo);
                ds = DataHelper.RepairWorkDao.complexSel(strWorkNo, strAssetNo, strDateFrom, strDateTo);
           
           // }
          /*  catch
            {
                /*string str_Sql = "select WONUM as woNum,LOCDESCRIPTION as locDescription,DESCRIPTION as description,case STATUS when '1' then '处理中' when '2' then '转单' when '3' then '报修' when '9' then '已处理' else '无法识别' end as status,STATUSDATE from repairWorkList "
                    + "where WONUM like '%" + strWorkNo + "%' and ASSETNUM like '%" + strAssetNo + "%'";
                ds = DataHelper.RepairWorkDao.
                ds = PublicMethods.SelectData(str_Sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                    {
                        if (Convert.ToDateTime(ds.Tables[0].Rows[index]["STATUSDATE"]) < Convert.ToDateTime(strDateFrom) || Convert.ToDateTime(ds.Tables[0].Rows[index]["STATUSDATE"]) > Convert.ToDateTime(strDateTo))
                        {
                            ds.Tables[0].Rows.RemoveAt(index);
                        }
                    }
                }
            }*/
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                queryGrid.Visible = false;
                queryGrid.TableStyles.Clear();
                queryGrid.Refresh();
                MessageBox.Show("无数据");
                return;
            }

            dt = ds.Tables[0];
            for (int k = 0; k < dt.Rows.Count; k++) 
            {
                if (dt.Rows[k]["status"].ToString() == "9") dt.Rows[k]["status"] = "已处理";
                else if (dt.Rows[k]["status"].ToString() == "1") dt.Rows[k]["status"] = "待处理";
                else if (dt.Rows[k]["status"].ToString() == "2") dt.Rows[k]["status"] = "转单";
                else if (dt.Rows[k]["status"].ToString() == "3") dt.Rows[k]["status"] = "报修";
                else if (dt.Rows[k]["status"].ToString() == "4") dt.Rows[k]["status"] = "待料";
                else if (dt.Rows[k]["status"].ToString() == "5") dt.Rows[k]["status"] = "领料";
                else dt.Rows[k]["status"] ="未定义状态";
            }
                queryGrid.DataSource = dt;
            DataGridTableStyle dts = new DataGridTableStyle();
            dts.MappingName = dt.TableName;

            DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
            col1.HeaderText = "工单编号";
            col1.MappingName = "woNum";
            col1.Width = 83;
            dts.GridColumnStyles.Add(col1);

            DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
            col2.HeaderText = "地址";
            col2.MappingName = "locDescription";
            col2.Width = 100;
            dts.GridColumnStyles.Add(col2);

            DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();
            col3.HeaderText = "描述";
            col3.MappingName = "description";
            col3.Width = 71;
            dts.GridColumnStyles.Add(col3);

            DataGridTextBoxColumn col4 = new DataGridTextBoxColumn();
            col4.HeaderText = "状态";
            col4.MappingName = "status";
            col4.Width = 60;
            dts.GridColumnStyles.Add(col4);

            queryGrid.TableStyles.Clear();
            queryGrid.TableStyles.Add(dts);
            queryGrid.Refresh();

        }
    }
}